package org.jeecg.modules.rooms.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.rooms.DTO.CesStockTypeDto;
import org.jeecg.modules.rooms.entity.CesStockType;
import org.jeecg.modules.rooms.mapper.CesStockTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 商品库存分类表 服务实现类
 * </p>
 *
 * @author Claude
 * @since 2023-03-08
 */
@Service
public class CesStockTypeServiceImpl extends ServiceImpl<CesStockTypeMapper, CesStockType> implements IService<CesStockType> {

    @Resource
    private CesStockTypeMapper stockTypeMapper;
    /**
     * 获取商品库存分类
     * @param hotelId
     * @return
     */
    public Result tree(String hotelId){
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //查询数据
        List<CesStockType> list = stockTypeMapper.selectList(Wrappers.<CesStockType>lambdaQuery()
                .eq(CesStockType::getHotelId,hotelId)
                .eq(CesStockType::getInvalid,false));
        list.forEach(v -> {
            nodeList.add(new TreeNode<>(v.getId(),v.getParentId(),v.getName(),0));
        });
        if(CollUtil.isEmpty(nodeList)) return Result.OK(new ArrayList<>());
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
        return Result.OK(treeList);
    }

    public List<Tree<String>> treeList(String hotelId){
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //查询数据
        List<CesStockType> list = stockTypeMapper.selectList(Wrappers.<CesStockType>lambdaQuery()
                .eq(CesStockType::getHotelId,hotelId)
                .eq(CesStockType::getInvalid,false));
        list.forEach(v -> {
            nodeList.add(new TreeNode<>(v.getId(),v.getParentId(),v.getName(),0));
        });
        if(CollUtil.isEmpty(nodeList)) return new ArrayList<>();
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
        return treeList;
    }


    /**
     * 获取顶级分类
     * @return
     */
    public Result getTopTypes(String hotelId){
        //查询数据
        List<CesStockType> list = stockTypeMapper.selectList(Wrappers.<CesStockType>lambdaQuery()
                .eq(CesStockType::getHotelId,hotelId)
                .eq(CesStockType::getState,true)
                .eq(CesStockType::getParentId,"0")
                .eq(CesStockType::getInvalid,false));
        return Result.OK(list);
    }
    /**
     * 获取下级分类
     * @return
     */
    public Result getChildTypes(String hotelId,String parentId){
        //查询数据
        List<CesStockType> list = stockTypeMapper.selectList(Wrappers.<CesStockType>lambdaQuery()
                .eq(CesStockType::getHotelId,hotelId)
                .eq(CesStockType::getState,true)
                .eq(CesStockType::getParentId,parentId)
                .eq(CesStockType::getInvalid,false));
        return Result.OK(list);
    }

    public Result getTopTypes(String hotelId,String posType) {
        LambdaQueryWrapper<CesStockType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CesStockType::getHotelId, hotelId);
        lambdaQueryWrapper.eq(CesStockType::getState, true);
        lambdaQueryWrapper.eq(CesStockType::getParentId, "0");
        lambdaQueryWrapper.eq(CesStockType::getInvalid, false);
        if (ObjectUtils.isNotEmpty(posType)) {
            lambdaQueryWrapper.like(CesStockType::getPosType, posType);
        }
        List<CesStockType> list = stockTypeMapper.selectList(lambdaQueryWrapper);
        return Result.OK(list);
    }

    /**
     * 创建分类
     * @param stockTypeDto
     * @return
     */
    public Result create(CesStockTypeDto stockTypeDto){
        QueryWrapper<CesStockType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(CesStockType.NAME,stockTypeDto.getName());
        queryWrapper.eq(CesStockType.HOTEL_ID, stockTypeDto.getHotelId());
        queryWrapper.eq(CesStockType.INVALID,false);

        CesStockType cesStockType = stockTypeMapper.selectOne(queryWrapper);
        if(!ObjectUtils.isEmpty(cesStockType)) return Result.error("已有相同名称的分类!");
        CesStockType stockType = new CesStockType();
        BeanUtil.copyProperties(stockTypeDto,stockType,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        stockType.setCreateAt(LocalDateTime.now());
        stockType.setUpdateAt(LocalDateTime.now());
        stockType.setInvalid(false);
        stockTypeMapper.insert(stockType);
        return Result.OK("保存成功!");
    }


    /**
     * 修改分类
     * @param stockTypeDto
     * @return
     */
    public Result modify(CesStockTypeDto stockTypeDto){
        CesStockType cesStockType = stockTypeMapper.selectById(stockTypeDto.getId());
        if(ObjectUtils.isEmpty(cesStockType)) return Result.error("数据未找到！");

        QueryWrapper<CesStockType> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(CesStockType.ID,cesStockType.getId());
        queryWrapper.eq(CesStockType.NAME,stockTypeDto.getName());
        queryWrapper.eq(CesStockType.HOTEL_ID, stockTypeDto.getHotelId());
        queryWrapper.eq(CesStockType.INVALID,false);

        CesStockType exit = stockTypeMapper.selectOne(queryWrapper);
        if(!ObjectUtils.isEmpty(exit)) return Result.error("已有相同名称的分类!");

        //忽略空值 不然会覆盖
        BeanUtil.copyProperties(stockTypeDto, cesStockType,CopyOptions.create().setIgnoreProperties(CesStockType.ID).setIgnoreError(true));
        cesStockType.setUpdateAt(LocalDateTime.now());
        stockTypeMapper.updateById(cesStockType);
        return Result.OK("修改成功!");
    }


    public Result delete(CesStockTypeDto stockTypeDto){
        CesStockType cesStockType = stockTypeMapper.selectById(stockTypeDto.getId());
        if(ObjectUtils.isEmpty(cesStockType)) return Result.error("数据未找到！");
        cesStockType.setInvalid(true);
        cesStockType.setUpdateAt(LocalDateTime.now());
        stockTypeMapper.updateById(cesStockType);
        return Result.OK("删除成功!");
    }


    /**
     * 根据IDS 查询分类数据
     * @param ids
     * @return
     */
    public List<CesStockType> fetchByIds(List<String> ids){
        if(CollectionUtil.isEmpty(ids)) return new ArrayList<>();
        List<CesStockType> list = stockTypeMapper.selectList(Wrappers.<CesStockType>lambdaQuery().eq(CesStockType::getInvalid,false).in(CesStockType::getId,ids));

        return  list;
    }




}
